package org.bendev.ildaplayer;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class ChannelListActivity extends Activity implements SolrAdapter.OnChannelsListener{
	public static final String 
		CHANNELS = "channels",
		CHANNEL = "channel";

	ArrayList<String> mChannels;
	ChannelListFragment mChannelList;

	@Override
	protected void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_channel_list );

		mChannelList = (ChannelListFragment) getFragmentManager().findFragmentById( R.id.channel_list );
		mChannelList.setOnChannelClickListener( new ChannelListFragment.OnChannelClickListener() {
			@Override
			public void onChannelClick( String channel ) {
				startActivity(
					new Intent( ChannelListActivity.this, ProgramListActivity.class )
						.putExtra( CHANNEL, channel )
						.putExtra( CHANNELS, mChannels )
				);
			}
		} );

		if( savedInstanceState!=null && savedInstanceState.containsKey( CHANNELS ) && savedInstanceState.get( CHANNELS )!=null )
			onChannels( savedInstanceState.getStringArrayList( CHANNELS ) );
		else
			SolrAdapter.getChannels( this );
	}		

	@Override
	public void onChannels( List<String> channels ) {
		mChannels = new ArrayList<String>( channels );
		mChannelList.setChannels( channels );
	}

	@Override
	protected void onSaveInstanceState( Bundle outState ){
		outState.putStringArrayList( CHANNELS, mChannels );
	}
}
